<template>
  <div>
    <!-- 搜索 -->
    <!-- 一般车辆配件类型保修期查询 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'" :onColumnsChange="columns => (this.columns = columns)" :is-select-column="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button type="primary" :click="exportFileBtn.bind()">下载</multiuse-button>
      </template>
    </FilterTable>
  </div>
</template>
<script>
import { getListByPage,getListForExport } from '@/api/claim/dealer/GenerallyCarPartTypeWarrantyQuery';
import { exportExcelFile } from '@/utils/index';
import { getPart } from '@/api/dictionary/index.js';
import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
       fetchapi: getListByPage,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false
    };
  },
  created() {
    this.chuqi()
  },
  methods: {
    // 导出
    async exportFileBtn() {
      this.loading1 = true;
      getListForExport(this.fetchParams).then(res => {
        exportExcelFile(res.data, '一般车辆备件类型保修期');
      });
      this.loading1 = false;
    },
    async chuqi(){
      const res = await getPart();
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'typeId').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'typeId').itemList = res.data.map(y => ({ text: y.name, value: y.code }));
      }
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
      console.log(val);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '保修期',
          fieldName: 'pRpairM',
          unitRender: () => {
            return <span>(月)</span>;
          },
          rules: [
            {
              validator: (rule, value, callback) => {
                if (value) {
                  if (Number.isInteger(Number(value))) {
                    callback();
                  } else {
                    callback(new Error('请输入数字值...'));
                  }
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change'],
            },
          ],
          placeholder: '请输入保修期...',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '保修里程',
          fieldName: 'pMile',
          unitRender: () => {
            return <span>(KM)</span>;
          },
          rules: [
            {
              validator: (rule, value, callback) => {
                if (value) {
                  if (Number.isInteger(Number(value))) {
                    callback();
                  } else {
                    callback(new Error('请输入数字值...'));
                  }
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change'],
            },
          ],
          placeholder: '请输入保修里程...',
          initialValue: '',
        },
        {
          type: 'SELECT', 
          label: '备件类型', 
          fieldName: 'typeId', 
          placeholder: '请选择备件类型...', 
          initialValue: '', 
          itemList: [
            { text: '三电', value: '55091001' },
            { text: '整车', value: '55091002' },
            { text: '动力总成', value: '55091003' },
            { text: '特殊零部件', value: '55091004' },
            { text: '易损件1', value: '55091005' },
            { text: '易损件2', value: '55091006' },
            { text: '备件保修期1', value: '55091007' },
            { text: '备件保修期2', value: '55091008' }
          ], 
          filterable: true
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'pStatus',
          placeholder: '请选择状态...',
          initialValue: '',
          itemList: [
            { text: '启用', value: 1 },
            { text: '禁用', value: 0 }
          ],
          filterable: true
        }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {title: '备件类型', dataIndex: 'typeName', sorter: true},
        { title: '车系', dataIndex: 'seriesName', sorter: true },
        { title: '营运类型', dataIndex: 'ptType', sorter: true },
        {title: '保修期（月）', dataIndex: 'pRpairM', sorter: true},
        {title: '保修里程（KM）', dataIndex: 'pMile', sorter: true},
        {title: '状态', dataIndex: 'pStatusStr', sorter: true},
        {title: '创建人', dataIndex: 'createdBy', sorter: true},
        {title: '创建时间', dataIndex: 'createdAt', sorter: true},
        {title: '修改人', dataIndex: 'updatedBy', sorter: true},
        {title: '修改时间', dataIndex: 'updatedAt', sorter: true}
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
